<template>
  <svg
    :class="`${svgClass} ${spin ? 'scoped-svg-animation' : ''}`"
    aria-hidden="true"
    v-on="$listeners"
  >
    <use :xlink:href="iconClass"></use>
  </svg>
</template>

<script>
export default {
  name: "icon-svg",
  props: {
    //  使用icon的图标类型
    iconName: {
      type: String,
      required: true
    },
    // 是否添加class样式
    className: {
      type: String,
      default: ""
    },
    // 是否旋转
    spin: {
      type: Boolean,
      default: false
    }
  },
  computed: {
    iconClass() {
      return `#icon-${this.iconName}`;
    },
    svgClass() {
      if (this.className) {
        return `icon ${this.className}`;
      } else {
        return `icon`;
      }
    }
  }
};
</script>

<style scoped lang="scss">
.icon {
  width: 1em;
  height: 1em;
  vertical-align: -0.15em;
  fill: currentColor;
  overflow: hidden;
  &.scoped-svg-animation {
    animation: rotate 2s linear infinite;
  }
  @keyframes rotate {
    from {
      transform: rotate(0deg);
    }
    to {
      transform: rotate(360deg);
    }
  }
}
</style>
